import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login'
import Layout from '../views/layout'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/layout',
    name: 'layout',
    component: Layout
  }
]

const router = new VueRouter({
  routes
})
/*
beforeEach 导航守卫
to 去往何处
from 从哪里来
next 控制去不去或者去到哪里，必须要调用，否则就会一直卡在这里
next()如果不加参数就是正常跳转到下一个页面
next()如果加了参数，就是跳转到指定页面
*/
// 访问的白名单，可以直接访问
const whiteList = ['/login']
router.beforeEach((to, form, next) => {
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (localStorage.getItem('heimamm-token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
